package edu.pku.sei.transformation.editor.views.figures;

import org.eclipse.draw2d.ColorConstants;
import org.eclipse.draw2d.ConnectionEndpointLocator;
import org.eclipse.draw2d.Label;
import org.eclipse.draw2d.PolygonDecoration;
import org.eclipse.draw2d.PolylineConnection;
import org.eclipse.draw2d.PolylineDecoration;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.draw2d.geometry.PointList;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;

public class ConnectionFigure extends PolylineConnection {
	public static final int CONNECTION_FIGURE_MIDDLE = 0;
	public static final int CONNECTION_FIGURE_SOURCE = 1;
	public static final int CONNECTION_FIGURE_TARGET = 2;
	
	private Label sourceLabel;
	private Label targetLabel;
	private Label middleLabel;
	private Label tooltip;

	public static final int NONE = 0;
	public static final int ARROW = 1;
	public static final int TRIANGLE = 2;
	public static final int DIAMOND = 3;
	
	public ConnectionFigure(int lineStyle) {
		this.setLineStyle(lineStyle);
	}
	
	public ConnectionFigure() {
		this.setLineStyle(SWT.LINE_SOLID);
	}
	
	private Label getLabel(int position) {
		if (position == CONNECTION_FIGURE_MIDDLE) {
			if (middleLabel == null) {
				middleLabel = new Label();
				MidPointLocator middleLocator = new MidPointLocator(this, new Point(0,
						-10));
				this.add(middleLabel, middleLocator);
			} 
			return middleLabel;
		} else if (position == CONNECTION_FIGURE_SOURCE) {
			if (sourceLabel == null) {
				sourceLabel = new Label();
				ConnectionEndpointLocator sourceLocator = new ConnectionEndpointLocator(
						this, false);
				this.add(sourceLabel, sourceLocator);				
			}
			return sourceLabel;
		} else if (position == CONNECTION_FIGURE_TARGET) {
			if (targetLabel == null) {
				targetLabel = new Label();
				ConnectionEndpointLocator targetLocator = new ConnectionEndpointLocator(
						this, true);
				this.add(targetLabel, targetLocator);				
			}
			return targetLabel;
		}
		return null;
	}
	
	public void setLabelIcon(Image img, int position) { 
		Label label = getLabel(position);
		if (label != null) {
			label.setIcon(img);
		}
	}
	
	
	public void setLabel(String s, int position) {
		Label label = getLabel(position);
		if (label != null) {
			label.setText(s);
		}
	}

	public void setTooltipLabel(String s) {
		if (tooltip == null) {
			tooltip = new Label();
			this.setToolTip(tooltip);
		}
		tooltip.setText(s);
	}
	
	public void setTargetDecoration(int style) {
		if (style == ARROW) {
			PolylineDecoration arrow;
			arrow = new PolylineDecoration();
			arrow.setScale(15, 5);
			
			setTargetDecoration(arrow);
		} else if (style == DIAMOND) {
			PolygonDecoration diamond;
			diamond = new PolygonDecoration();
			PointList decorationPointList;
			diamond = new PolygonDecoration();
			decorationPointList = new PointList();
			decorationPointList.addPoint(0,0);
			decorationPointList.addPoint(-3,2);
			decorationPointList.addPoint(-6,0);
			decorationPointList.addPoint(-3,-2);
			diamond.setTemplate(decorationPointList);
			diamond.setScale(3, 3);
			
			setTargetDecoration(diamond);
		} else if (style == TRIANGLE) {
			PolygonDecoration triangle;
			triangle = new PolygonDecoration();
			triangle.setScale(15, 5);
			triangle.setBackgroundColor(ColorConstants.white);
			
			setTargetDecoration(triangle);
		} else {
			setTargetDecoration(null);
		}
	}
	
	public void setSourceDecoration(int style) {
		if (style == ARROW) {
			PolylineDecoration arrow;
			arrow = new PolylineDecoration();
			arrow.setScale(15, 5);
			
			setSourceDecoration(arrow);
		} else if (style == DIAMOND) {
			PolygonDecoration diamond;
			diamond = new PolygonDecoration();
			PointList decorationPointList;
			diamond = new PolygonDecoration();
			decorationPointList = new PointList();
			decorationPointList.addPoint(0,0);
			decorationPointList.addPoint(-3,2);
			decorationPointList.addPoint(-6,0);
			decorationPointList.addPoint(-3,-2);
			diamond.setTemplate(decorationPointList);
			diamond.setScale(3, 3);
			
			setSourceDecoration(diamond);
		} else if (style == TRIANGLE) {
			PolygonDecoration triangle;
			triangle = new PolygonDecoration();
			triangle.setScale(15, 5);
			triangle.setBackgroundColor(ColorConstants.white);
			
			setSourceDecoration(triangle);
		} else {
			setSourceDecoration(null);
		}
	}
}
